home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / UDP.C < prev    next >
Text File  |  1993-10-18  |  8KB  |  365 lines

  1. /* UDP-related user commands and other functions
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  *
  4.  * put into one file - DB3FL.920912
  5.  */
  6.  
  7. #include "global.h"
  8. #include "config.h"
  9. #ifdef UDP
  10. #include "mbuf.h"
  11. #include "netuser.h"
  12. #include "iface.h"
  13. #include "udp.h"
  14. #include "ip.h"
  15. #include "internet.h"
  16. #include "icmp.h"
  17. #include "cmdparse.h"
  18.  
  19. /* UDP control structures list */
  20. static struct udp_cb *Udps;
  21.  
  22. static struct mib_entry Udp_mib[] = {
  23.     "",                0,
  24.     "InDatagrams",    0,
  25.     "NoPorts",        0,
  26.     "InErrors",        0,
  27.     "OutDatagrams",    0,
  28. };
  29.  
  30. /* Look up UDP socket.
  31.  * Return control block pointer or NULLUDP if nonexistant
  32.  * As side effect, move control block to top of list to speed future
  33.  * searches.
  34.  */
  35. static struct udp_cb * near
  36. lookup_udp(struct socket *socket)
  37. {
  38.     struct udp_cb *up, *uplast = NULLUDP;
  39.  
  40.     for(up = Udps; up != NULLUDP; uplast = up, up = up->next) {
  41.         if(socket->port == up->socket.port
  42.           && (socket->address == up->socket.address
  43.           || up->socket.address == INADDR_ANY)) {
  44.             if(uplast != NULLUDP) {
  45.                 /* Move to top of list */
  46.                 uplast->next = up->next;
  47.                 up->next = Udps;
  48.                 Udps = up;
  49.             }
  50.             return up;
  51.         }
  52.     }
  53.     return NULLUDP;
  54. }
  55.  
  56. /* Create a UDP control block for lsocket, so that we can queue
  57.  * incoming datagrams.
  58.  */
  59. struct udp_cb *
  60. open_udp(struct socket *lsocket,void (*r_upcall)())
  61. {
  62.     struct udp_cb *up;
  63.  
  64.     if((up = lookup_udp(lsocket)) == NULLUDP) {
  65.         up = mxallocw(sizeof (struct udp_cb));
  66.  
  67.         up->socket.address = lsocket->address;
  68.         up->socket.port = lsocket->port;
  69.         up->r_upcall = r_upcall;
  70.  
  71.         up->next = Udps;
  72.         Udps = up;
  73.     }
  74.     return up;
  75. }
  76.  
  77. /* Send a UDP datagram */
  78. int
  79. send_udp(
  80. struct socket *lsocket,        /* Source socket */
  81. struct socket *fsocket,        /* Destination socket */
  82. char tos,                    /* Type-of-service for IP */
  83. char ttl,                    /* Time-to-live for IP */
  84. struct mbuf *data,            /* Data field, if any */
  85. int16 length,                /* Length of data field */
  86. int16 id,                    /* Optional ID field for IP */
  87. char df)                    /* Don't Fragment flag for IP */
  88. {
  89.     struct mbuf *bp;
  90.     struct pseudo_header ph;
  91.     struct udp udp;
  92.     int32 laddr = lsocket->address;
  93.  
  94.     if(length != 0 && data != NULLBUF) {
  95.         trim_mbuf(&data,length);
  96.     } else {
  97.         length = len_p(data);
  98.     }
  99.     length += UDPHDR;
  100.  
  101.     if(laddr == INADDR_ANY) {
  102.         laddr = locaddr(fsocket->address);
  103.     }
  104.     udp.source = lsocket->port;
  105.     udp.dest = fsocket->port;
  106.     udp.length = length;
  107.  
  108.     /* Create IP pseudo-header, compute checksum and send it */
  109.     ph.length = length;
  110.     ph.source = laddr;
  111.     ph.dest = fsocket->address;
  112.     ph.protocol = UDP_PTCL;
  113.  
  114.     udpOutDatagrams++;
  115.     bp = htonudp(&udp,data,&ph);
  116.  
  117.     ip_send(laddr,fsocket->address,UDP_PTCL,tos,ttl,bp,length,id,df);
  118.     return (int)length;
  119. }
  120.  
  121. /* Accept a waiting datagram, if available. Returns length of datagram */
  122. int
  123. recv_udp(
  124. struct udp_cb *up,
  125. struct socket *fsocket,        /* Place to stash incoming socket */
  126. struct mbuf **bp)            /* Place to stash data packet */
  127. {
  128.     struct socket sp;
  129.     struct mbuf *buf;
  130.     int16 length;
  131.  
  132.     if(up == NULLUDP){
  133.         return -1;
  134.     }
  135.     if(up->rcvcnt == 0){
  136.         return -1;
  137.     }
  138.     buf = dequeue(&up->rcvq);
  139.     up->rcvcnt--;
  140.  
  141.     /* Strip socket header */
  142.     pullup(&buf,(char *)&sp,sizeof(struct socket));
  143.  
  144.     /* Fill in the user's foreign socket structure, if given */
  145.     if(fsocket != NULLSOCK) {
  146.         fsocket->address = sp.address;
  147.         fsocket->port = sp.port;
  148.     }
  149.     /* Hand data to user */
  150.     length = len_p(buf);
  151.  
  152.     if(bp != NULLBUFP) {
  153.         *bp = buf;
  154.     } else {
  155.         free_p(buf);
  156.     }
  157.     return (int)length;
  158. }
  159.  
  160. /* Delete a UDP control block */
  161. int
  162. del_udp(struct udp_cb *conn)
  163. {
  164.     struct mbuf *bp;
  165.     struct udp_cb *up, *udplast = NULLUDP;
  166.  
  167.     for(up = Udps; up != NULLUDP; udplast = up, up = up->next) {
  168.         if(up == conn) {
  169.             break;
  170.         }
  171.     }
  172.     if(up == NULLUDP) {
  173.         /* Either conn was NULL or not found on list */
  174.         return -1;
  175.     }
  176.     /* Get rid of any pending packets */
  177.     while(up->rcvcnt != 0) {
  178.         bp = up->rcvq;
  179.         up->rcvq = up->rcvq->anext;
  180.         free_p(bp);
  181.         up->rcvcnt--;
  182.     }
  183.     /* Remove from list */
  184.     if(udplast != NULLUDP) {
  185.         udplast->next = up->next;
  186.     } else {
  187.         Udps = up->next;    /* was first on list */
  188.     }
  189.     xfree(up);
  190.     return 0;
  191. }
  192.  
  193. /* Process an incoming UDP datagram */
  194. void
  195. udp_input(
  196. struct iface *iface,        /* Input interface */
  197. struct ip *ip,                /* IP header */
  198. struct mbuf *bp,            /* UDP header and data */
  199. int rxbroadcast)            /* The only protocol that accepts 'em */
  200. {
  201.     struct pseudo_header ph;
  202.     struct udp udp;
  203.     struct udp_cb *up;
  204.     struct socket lsocket, fsocket;
  205.     struct mbuf *packet;
  206.     int16 length = ip->length - IPLEN - ip->optlen;
  207.  
  208.     if(bp == NULLBUF) {
  209.         return;
  210.     }
  211.     /* Create pseudo-header and verify checksum */
  212.     ph.source = ip->source;
  213.     ph.dest = ip->dest;
  214.     ph.protocol = ip->protocol;
  215.     ph.length = length;
  216.  
  217.     /* Peek at header checksum before we extract the header. This
  218.      * allows us to bypass cksum() if the checksum field was not
  219.      * set by the sender.
  220.      */
  221.     if((udp.checksum = udpcksum(bp)) != 0 && cksum(&ph,bp,length) != 0){
  222.         /* Checksum non-zero, and wrong */
  223.         udpInErrors++;
  224.         free_p(bp);
  225.         return;
  226.     }
  227.     /* Extract UDP header in host order */
  228.     if(ntohudp(&udp,&bp) != 0){
  229.         /* Truncated header */
  230.         udpInErrors++;
  231.         free_p(bp);
  232.         return;
  233.     }
  234.     /* If this was a broadcast packet, pretend it was sent to us */
  235.     lsocket.address = (rxbroadcast) ? iface->addr : ip->dest;
  236.     lsocket.port = udp.dest;
  237.  
  238.     /* See if there's somebody around to read it */
  239.     if((up = lookup_udp(&lsocket)) == NULLUDP){
  240.         /* Nope, return an ICMP message */
  241.         if(!rxbroadcast){
  242.             bp = htonudp(&udp,bp,&ph);
  243.             icmp_output(ip,bp,ICMP_DEST_UNREACH,ICMP_PORT_UNREACH,NULLICMP);
  244.         }
  245.         udpNoPorts++;
  246.         free_p(bp);
  247.         return;
  248.     }
  249.     /* Create space for the foreign socket info */
  250.     packet = pushdown(bp,sizeof(struct socket));
  251.  
  252.     fsocket.address = ip->source;
  253.     fsocket.port = udp.source;
  254.     memcpy(&packet->data[0],(char *)&fsocket,sizeof(struct socket));
  255.  
  256.     /* Queue it */
  257.     enqueue(&up->rcvq,packet);
  258.     up->rcvcnt++;
  259.     udpInDatagrams++;
  260.  
  261.     if(up->r_upcall) {
  262.         (*up->r_upcall)(iface,up,up->rcvcnt);
  263.     }
  264. }
  265.  
  266. int
  267. st_udp(struct udp_cb *udp)
  268. {
  269.     return tprintf("%lx%6u  %s\n",ptol(udp),udp->rcvcnt,pinet(&udp->socket));
  270. }
  271.  
  272. /* Convert UDP header in internal format to an mbuf in external format */
  273. static struct mbuf *
  274. htonudp(struct udp *udp,struct mbuf *data,struct pseudo_header *ph)
  275. {
  276.     int16 checksum;
  277.  
  278.     /* Allocate UDP protocol header and fill it in */
  279.     struct mbuf *bp = pushdown(data,UDPHDR);
  280.     char *cp = bp->data;
  281.  
  282.     cp = put16(cp,udp->source);                /* Source port */
  283.     cp = put16(cp,udp->dest);                /* Destination port */
  284.     cp = put16(cp,udp->length);                /* Length */
  285.     *cp++ = 0;                                /* Clear checksum */
  286.     *cp-- = 0;
  287.  
  288.     /* All zeros and all ones is equivalent in one's complement arithmetic;
  289.      * the spec requires us to change zeros into ones to distinguish an
  290.       * all-zero checksum from no checksum at all
  291.      */
  292.     if((checksum = cksum(ph,bp,ph->length)) == 0) {
  293.         checksum = 0xffffffffL;
  294.     }
  295.     put16(cp,checksum);
  296.     return bp;
  297. }
  298.  
  299. /* Convert UDP header in mbuf to internal structure */
  300. int
  301. ntohudp(struct udp *udp,struct mbuf **bpp)
  302. {
  303.     char udpbuf[UDPHDR];
  304.  
  305.     if(pullup(bpp,udpbuf,UDPHDR) != UDPHDR) {
  306.         return -1;
  307.     }
  308.     udp->source = get16(&udpbuf[0]);
  309.     udp->dest = get16(&udpbuf[2]);
  310.     udp->length = get16(&udpbuf[4]);
  311.     udp->checksum = get16(&udpbuf[6]);
  312.     return 0;
  313. }
  314.  
  315. /* Extract UDP checksum value from a network-format header without
  316.  * disturbing the header
  317.  */
  318. static int16
  319. udpcksum(struct mbuf *bp)
  320. {
  321.     struct mbuf *dup;
  322.  
  323.     if(dup_p(&dup,bp,6,2) != 2) {
  324.         return 0;
  325.     }
  326.     return pull16(&dup);
  327. }
  328.  
  329.  
  330. /* ------------------------------ UDP sub cmds -------------------------- */
  331.  
  332. /* Dump UDP statistics and control blocks */
  333. static int
  334. doudpstat(int argc,char **argv,void *p)
  335. {
  336.     struct udp_cb *udp;
  337.     int i;
  338.  
  339.     for(i = 1; i <= NUMUDPMIB; i++) {
  340.         tprintf("(%2u)udp%-17s%10lu",i,Udp_mib[i].name,Udp_mib[i].value.integer);
  341.         tputs((i % 2) ? "     " : "\n");
  342.     }
  343.     if((i % 2) == 0) {
  344.         tputs("\n");
  345.     }
  346.     tputs("&UCB Rcv-Q  Local socket\n");
  347.  
  348.     for(udp = Udps;udp != NULLUDP; udp = udp->next) {
  349.         st_udp(udp);
  350.     }
  351.     return 0;
  352. }
  353.  
  354. int
  355. doudp(int argc,char **argv,void *p)
  356. {
  357.     struct cmds Udpcmds[] = {
  358.         "status",    doudpstat,    0, 0,    NULLCHAR,
  359.         NULLCHAR,
  360.     };
  361.     return subcmd(Udpcmds,argc,argv,p);
  362. }
  363.  
  364. #endif /* UDP */
  365.